﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;

namespace FCTC_Web.Utilities
{
    public class XmlHelper
    {
        public static string FileName { get; set; }

        static XmlDocument _Doc;
        public static XmlDocument Doc
        {
            get
            {
                if (_Doc == null)
                {
                    _Doc = new XmlDocument();
                    if(string.IsNullOrWhiteSpace(FileName))
                        throw new ArgumentNullException("xml path is null");
                   string path = AppDomain.CurrentDomain.BaseDirectory;
                   _Doc.Load(path + "App_Data\\"+FileName);
                }
                return _Doc;
            }

        }

        public static XmlNodeList GetNode(string tagName)
        {
           var nodes =  Doc.GetElementsByTagName(tagName);
           return nodes;
        }
    }

    public class VehicleXmlHelper 
    {
        static List<string> _Brands;
        public static List<string> Brands
        {
            get
            { 
                if(_Brands == null)
                {
                    _Brands = new List<string>();
                    var nodes = GetBrandNode();
                    for(int i=0;i<nodes.Count;i++)
                    {
                        var item = nodes.Item(i);
                        _Brands.Add(item.Attributes["name"].Value);
                    }
                }
                return _Brands;
            }
        }

        public static List<string> GetSelectiosn(string brandName)
        {
            var xpathSelection = "//vehicle/brand[@name='" + brandName + "']";
          //  var xpathSelection = "//vehicle/brand";
            var brand = XmlHelper.Doc.SelectSingleNode(xpathSelection);

            if (brand == null)
                return new List<string>();

            var selectionNodes = brand.ChildNodes;
            var sl = new List<string>();
            for (int i = 0; i < selectionNodes.Count; i++)
            {
                var item = selectionNodes.Item(i);
                sl.Add(item.Attributes["name"].Value);
            }

            return sl;
        }


        public static XmlNodeList GetBrandNode()
        {
            return XmlHelper.GetNode("brand");
        }

        internal static List<string> GetvehicleType(string brandName, string selectionName)
        {
            var xpathSelection = "//vehicle/brand[@name='" + brandName + "']/Selection[@name='"+selectionName+"']";
            //  var xpathSelection = "//vehicle/brand";
            var selection = XmlHelper.Doc.SelectSingleNode(xpathSelection);

            if (selection == null)
                return new List<string>();

            var types = selection.ChildNodes;
            var sl = new List<string>();
            for (int i = 0; i < types.Count; i++)
            {
                var item = types.Item(i);
                sl.Add(item.Attributes["name"].Value);
            }

            return sl;

        }
    }
}